package crm.view.setting;

import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;

import org.eclipse.wb.swing.FocusTraversalOnArray;

import crm.model.Setting;
import crm.model.teacherPC.Manager;
import crm.util.TitleBorder;
import crm.view.setting.addremovelist.ListPan;
import crm.view.setting.listener.BroadcastFocusListener;
import crm.view.setting.listener.ConnectPortListener;
import crm.view.setting.listener.PropertiesFilter;
import crm.view.setting.listener.TimeListener;
import crm.view.setting.listener.TransferPortListener;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class SettingPane extends JPanel implements ActionListener{
	Manager manager;
	private JButton jButton2;
	private JTextField dataPathField;
	private JLabel jLabel5;
	private JTextField dataNameField;
	private JLabel jLabel4;
	private JPanel dataPan;

	private JLabel jLabel3;
	private JTextField jTextField2;
	private JLabel mathNrPath;
	private JTextField mathNrField;
	private JLabel matNrName;
	private JPanel matNrPan;
	private JSpinner jSpinner3;
	private JSpinner jSpinner2;
	private JSpinner jSpinner1;
	private JButton exportBut;
	private JLabel jLabel2;
	private JLabel jLabel1;
	private JLabel conportLab;
	
	private JPanel timeBroadcastPan;
	private JPanel portPan;
	
	private JPanel timePan;
	private JComboBox uebertragungscombobox;
	private JLabel uebertragungsLab;
	private JTextField ipField;
	private JLabel ip;

	private JButton jButton1;
	JFrame gui;

	private JTextField jTextField1;
	private ListPan listPan;
	private ListPan listBrowserPan;
	
	public SettingPane(JFrame gui, Manager manager){
		this.manager=manager;
		this.gui=gui;
		createGUI();
		setBoder();
	}
	
	public void createGUI() {

		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[] {100, 106, 0, 12, 0, 69, 0};
		gridBagLayout.rowHeights = new int[] {23, 37, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7};
		gridBagLayout.columnWeights = new double[] {0.0, 0.0, 0.0, 0.1, 0.0, 0.1, 0.0};
		gridBagLayout.rowWeights = new double[] {0.0, 0.0, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1};
		
		setLayout(gridBagLayout);
		this.setPreferredSize(new java.awt.Dimension(662, 348));
		{
			timeBroadcastPan = new JPanel();
			add(timeBroadcastPan, new GridBagConstraints(2, 1, 3, 2, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));

			GridLayout timeBroadcastPanLayout = new GridLayout(2, 2);
			timeBroadcastPanLayout.setColumns(1);
			timeBroadcastPanLayout.setHgap(5);
			timeBroadcastPanLayout.setVgap(5);
			timeBroadcastPan.setLayout(timeBroadcastPanLayout);
			{
				jLabel3 = new JLabel();
				timeBroadcastPan.add(jLabel3);
				jLabel3.setText("Broadcast-Address");
			}
			{
				jTextField1 = new JTextField(Setting.getBroadcastAddress());
				timeBroadcastPan.add(jTextField1);
				jTextField1.addFocusListener(new BroadcastFocusListener(jTextField1, gui));
			}
			{
				ip = new JLabel();
				timeBroadcastPan.add(ip);
				ip.setText("IP-Addresse");
			}
			{
				ipField = new JTextField();
				ipField.setEditable(false);
				timeBroadcastPan.add(ipField);
			}
		}
		timePan = new JPanel();
		
		
					GridLayout timePanLayout = new GridLayout(2, 2);
					timePanLayout.setColumns(1);
					timePanLayout.setHgap(5);
					timePanLayout.setVgap(5);
					timePan.setLayout(timePanLayout);
					add(timePan, new GridBagConstraints(5, 1, 2, 2, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 5, 0), 0, 0));
					{
						jLabel2 = new JLabel();
						timePan.add(jLabel2);
						jLabel2.setText("Zeitintervall");
						jLabel2.setPreferredSize(new java.awt.Dimension(99, 23));
					}
					jSpinner3 = new JSpinner();
					timePan.add(jSpinner3);
					jSpinner3.setModel(new SpinnerNumberModel(Setting.getAktualisatorTime(), 1000, 9999999, 100));
					jSpinner3.addChangeListener(new TimeListener());
					jSpinner3.setToolTipText("Zeit bis zu Refresh");
					jSpinner3.setPreferredSize(new java.awt.Dimension(99, 11));
					{
						uebertragungsLab = new JLabel();
						timePan.add(uebertragungsLab);
						uebertragungsLab.setText("VNC Qualit\u00E4t");
					}
					uebertragungscombobox = new JComboBox();
					timePan.add(uebertragungscombobox);
					uebertragungscombobox.setModel(new DefaultComboBoxModel(new Object[]{"low", "medium", "high"}));
		{
			matNrPan = new JPanel();
			GridLayout matNrPanLayout = new GridLayout(2, 2);
			matNrPanLayout.setHgap(5);
			matNrPanLayout.setVgap(5);
			matNrPanLayout.setColumns(1);
			matNrPan.setLayout(matNrPanLayout);
			this.add(matNrPan, new GridBagConstraints(0, 4, 2, 2, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
			{
				matNrName = new JLabel();
				matNrPan.add(matNrName);
				matNrName.setText("MatNr Name");
			}
			{
				mathNrField = new JTextField(Setting.getMathNrName());
				matNrPan.add(mathNrField);
			}
			{
				mathNrPath = new JLabel(Setting.getMathNrPath());
				matNrPan.add(mathNrPath);
				mathNrPath.setText("MatNr Path");
			}
			{
				jTextField2 = new JTextField();
				matNrPan.add(jTextField2);
			}
		}
		{
			dataPan = new JPanel();
			GridLayout dataPanLayout = new GridLayout(2, 2);
			dataPanLayout.setHgap(5);
			dataPanLayout.setVgap(5);
			dataPanLayout.setColumns(1);
			dataPan.setLayout(dataPanLayout);
			this.add(dataPan, new GridBagConstraints(2, 4, 3, 2, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
			{
				jLabel4 = new JLabel();
				dataPan.add(jLabel4);
				jLabel4.setText("XML-Name");
			}
			{
				dataNameField = new JTextField();
				dataPan.add(dataNameField);
				dataNameField.setText(Setting.getDataName());
			}
			{
				jLabel5 = new JLabel();
				dataPan.add(jLabel5);
				jLabel5.setText("XML-Pfand");
			}
			{
				dataPathField = new JTextField();
				dataPan.add(dataPathField);
				dataPathField.setText(Setting.getDataPath());
			}
		}
		{
			listPan = new ListPan(gui, manager,manager.appbannList);
			this.add(listPan, new GridBagConstraints(0, 8, 2, 3, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
		}
		{
			listBrowserPan = new ListPan(gui, manager,manager.browserbannList);
			this.add(listBrowserPan, new GridBagConstraints(2, 8, 3, 3, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
		}
		{
			exportBut = new JButton();
			add(exportBut, new GridBagConstraints(0, 13, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
			exportBut.setText("Einstellungen Exportieren");
			exportBut.addActionListener(this);
		}
		{
			portPan = new JPanel();

			GridLayout portPanLayout = new GridLayout(2, 2);
			portPanLayout.setColumns(1);
			portPanLayout.setHgap(5);
			portPanLayout.setVgap(5);
			portPan.setLayout(portPanLayout);
			add(portPan, new GridBagConstraints(0, 1, 2, 2, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
			{
				jLabel1 = new JLabel();
				portPan.add(jLabel1);
				jLabel1.setText("Transfer Port");
			}
			{
				SpinnerNumberModel jSpinner2Model =
					new SpinnerNumberModel(Setting.getTransportPort(), 1000, 9999, 1);
				jSpinner2 = new JSpinner();
				portPan.add(jSpinner2);
				jSpinner2.setModel(jSpinner2Model);
				jSpinner2.addChangeListener(new TransferPortListener());
			}
			{
				conportLab = new JLabel();
				portPan.add(conportLab);
				conportLab.setText("Send Port");
			}
			{
				SpinnerNumberModel jSpinner1Model =
					new SpinnerNumberModel(Setting.getSendPort(), 1000,9999,1);
				jSpinner1 = new JSpinner();
				portPan.add(jSpinner1);
				jSpinner1.setModel(jSpinner1Model);
				jSpinner1.addChangeListener(new ConnectPortListener());
			}
		}
		{
			{
				SpinnerNumberModel jSpinner3Model =
					new SpinnerNumberModel(Setting.getAktualisatorTime(),2000,5000000,100);
			}
			{
				ComboBoxModel uebertragungscomboboxModel =
					new DefaultComboBoxModel(
							new String[] { "High", "Medium", "Low" });
			}
		}
		{
			jButton1 = new JButton();
			add(jButton1, new GridBagConstraints(2, 13, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 5, 5), 0, 0));
			jButton1.setText("Einstellungen Importieren");
			jButton1.addActionListener(this);
		}
		setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[]{timeBroadcastPan, timePan, matNrPan, dataPan, jTextField1, ipField, jSpinner3, uebertragungscombobox, mathNrField, jTextField2, dataNameField, dataPathField, listPan, listBrowserPan, jLabel3, ip, jLabel2, uebertragungsLab, matNrName, mathNrPath, jLabel4, jLabel5}));
	}	
	
	public void setBoder() {
		timePan.setBorder(new TitleBorder(null, "Zeit", 3, 4));
		portPan.setBorder(new TitleBorder(null, "Ports", 3, 4));
		timeBroadcastPan.setBorder(new TitleBorder(null, "Time and Broadcast", 3, 4));
		matNrPan.setBorder(new TitleBorder(null, "MatNr", 3,4));
		dataPan.setBorder(new TitleBorder(null, "XML", 3,4));
		listPan.setBorder(new TitleBorder(null, "Banned Apps", 3,4));
		listBrowserPan.setBorder(new TitleBorder(null, "Banned Browser", 3,4));
	}

	
	public void actionPerformed(ActionEvent ev) {
		if (ev.getActionCommand().equals("Einstellungen Exportieren")){
			File settingFile = Setting.getFile();
			JFileChooser chooser = new JFileChooser();
	
			chooser.setFileFilter(new PropertiesFilter());
			if (chooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION){
				File file = chooser.getSelectedFile();
				if (!file.exists()){
					try {
						file.createNewFile();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				FileReader fRead =null;
				FileWriter fWrite = null ;
				try {
					fRead = new FileReader(settingFile);
					fWrite = new FileWriter(file);
					int ch = -1;
					while((ch = fRead.read()) != -1){
						fWrite.write(ch);
					}
				}catch(Exception e){
					e.printStackTrace();
				}finally {
					try {
						fRead.close();
						fWrite.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
	
				}
			}
		} else if (ev.getActionCommand().equals("Einstellungen Importieren")){
			JFileChooser chooser = new JFileChooser();
	
			chooser.setFileFilter(new PropertiesFilter());
			if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION){
				File file = chooser.getSelectedFile();
				Setting.importProperties(file);
			}
		}
	}
}
